﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using TraitorAmongUs.Common;

namespace TraitorAmongUs.States {
	public class MainMenuState : Singleton<MainMenuState>, GameState {
		Engine engine;

		Nuclex.UserInterface.Controls.LabelControl testLabel;
		Nuclex.UserInterface.Controls.Desktop.ButtonControl startButton;

		Nuclex.UserInterface.Controls.Desktop.InputControl serverIPTextBox;
		Nuclex.UserInterface.Controls.Desktop.ButtonControl joinServerButton;

		public MainMenuState() {
		}

		void GameState.EnterState(Engine e, dynamic param) {
			engine = e;

			engine.SetCursorVisibility(true);

			// Create the Main Menu GUI
			testLabel = new Nuclex.UserInterface.Controls.LabelControl("Main Menu");
			testLabel.Bounds = new Nuclex.UserInterface.UniRectangle(10.0f, 15.0f, 110.0f, 30.0f);
			engine.guiManager.Screen.Desktop.Children.Add(testLabel);

			startButton = new Nuclex.UserInterface.Controls.Desktop.ButtonControl();
			startButton.Bounds = new Nuclex.UserInterface.UniRectangle(10.0f, 50.0f, 110.0f, 30.0f);
			startButton.Text = "Host";
			startButton.Pressed += new EventHandler(startButton_Pressed);
			engine.guiManager.Screen.Desktop.Children.Add(startButton);

			serverIPTextBox = new Nuclex.UserInterface.Controls.Desktop.InputControl();
			serverIPTextBox.Bounds = new Nuclex.UserInterface.UniRectangle(10.0f, 110.0f, 300.0f, 30.0f);
			engine.guiManager.Screen.Desktop.Children.Add(serverIPTextBox);

			joinServerButton = new Nuclex.UserInterface.Controls.Desktop.ButtonControl();
			joinServerButton.Bounds = new Nuclex.UserInterface.UniRectangle(310.0f, 110.0f, 110.0f, 30.0f);
			joinServerButton.Text = "Join";
			joinServerButton.Pressed += new EventHandler(joinServerButton_Pressed);
			engine.guiManager.Screen.Desktop.Children.Add(joinServerButton);
		}

		void startButton_Pressed(object sender, EventArgs e) {
			// Change the state
			//engine.stateManager.SetState(States.PlayingState.Instance, null);
			engine.stateManager.SetState(States.LobbyState.Instance, new { Hosting = true });
		}

		void joinServerButton_Pressed(object sender, EventArgs e) {
			string serverHost = serverIPTextBox.Text;

			engine.stateManager.SetState(States.LobbyState.Instance, new { Hosting = false, ServerHost = serverHost, ServerPort = 1337 });
		}

		void GameState.LeaveState() {
			// Unload the GUI
			//engine.guiManager.Screen.Desktop.Children.Remove(testLabel);
			//engine.guiManager.Screen.Desktop.Children.Remove(startButton);
			engine.guiManager.Screen.Desktop.Children.Clear();
		}

		void GameState.Draw(GameTime gameTime) {
			//engine.GraphicsDevice.Clear(Color.Black);
		}

		void GameState.Update(GameTime gameTime) {
		}
	}
}
